import gtk, pygtk
pygtk.require('2.0')
import Board, DrawingController, Spec, Point,math,EntryDialog

class Screen(gtk.Window):
	
	def __init__(self):
	
		gtk.Window.__init__(self)
		self.connect("delete_event", gtk.main_quit)
		self.set_title("Pellegrino's first bimester")
		
		background = gtk.VBox() #contentPane
		self.add(background)

		self.box_options = gtk.HBox() # we'll place the options to paint here
		self.canvas_box = Board.Board()  # place to draw		
		background.pack_start(self.box_options)
		background.pack_start(gtk.HSeparator())
		background.pack_start(self.canvas_box)
		
		#for the click eventes
		self.canvas_box.connect("button_press_event", self.__clicked_board)
		#for the move events
		self.canvas_box.connect("motion_notify_event", self.__move_on_board)

		self.__put_options(self.box_options)
		self.drawing_controller = DrawingController.DrawingController(self.canvas_box)
		self.operation  = "none"
		self.selecting = False
############################### Constructing the Screen ##########################################################
	def __put_options(self, box):
		"""Put the option on top of the screen"""
		self.__put_drawing_generals(box)
		box.pack_start(gtk.VSeparator(), False)
		self.__put_shapes(box)
		box.pack_start(gtk.VSeparator(), False)
		self.__put_trace(box)
		box.pack_start(gtk.VSeparator(), False)
		self.__put_colors(box)
		box.pack_start(gtk.VSeparator(), False)
		self.__put_actions(box)

	
	def __put_drawing_generals(self, box):
		""" Putting draw and clear all"""
		boxV = gtk.VBox()
		box.pack_start(boxV)
		
		self.save_button = gtk.Button("Save")
		self.save_button.connect("clicked", self.__clicked_save)
		boxV.pack_start(self.save_button)
		
		self.delete_button = gtk.Button("Delete")
		self.delete_button.connect("clicked", self.__clicked_delete)
		boxV.pack_start(self.delete_button)		
		
		self.clear_button = gtk.Button("Clear All")
		self.clear_button.connect("clicked", self.__clicked_clearall)
		boxV.pack_start(self.clear_button)		

		
	def __put_shapes(self, box):
		""" Putting Shapes """
		boxV = gtk.VBox()
		box.pack_start(boxV)
		boxH = gtk.HBox()
		box1 = gtk.VBox()
		box2 = gtk.VBox()
		boxH.pack_start(box1)
		boxH.pack_start(box2)
		
		self.radio_line = gtk.RadioButton(None, "Line")
		self.radio_circle = gtk.RadioButton(self.radio_line, "Circle")
		self.radio_polygon = gtk.RadioButton(self.radio_line, "Polygon")
		box1.pack_start(self.radio_line)
		box1.pack_start(self.radio_circle)
		box1.pack_start(self.radio_polygon)
		
		self.radio_regular = gtk.RadioButton(self.radio_line, "Regular")
		self.radio_arrow = gtk.RadioButton(self.radio_line, "Arrow")
		box2.pack_start(self.radio_regular)
		box2.pack_start(self.radio_arrow)
		
		boxV.pack_start(gtk.Label("Shapes"))
		boxV.pack_start(boxH)
		
	def __put_trace(self, box):		
		""" Putting Trace """
		boxV = gtk.VBox()
		box.pack_start(boxV)
		self.check_double = gtk.CheckButton("Double")
		self.radio_normal = gtk.RadioButton(None, "Normal")
		self.radio_dotted = gtk.RadioButton(self.radio_normal, "Dotted")
		self.radio_dashed = gtk.RadioButton(self.radio_dotted, "Dashed")
		boxV.pack_start(gtk.Label("Trace"))
		boxV.pack_start(self.check_double)
		boxV.pack_start(self.radio_normal)
		boxV.pack_start(self.radio_dotted)
		boxV.pack_start(self.radio_dashed)

		
	def __put_colors(self, box):	
		""" Putting colors """
		boxV = gtk.VBox()
		box.pack_start(boxV)
		self.check_red = gtk.CheckButton("Red")
		self.check_green = gtk.CheckButton("Green")
		self.check_blue = gtk.CheckButton("Blue")
		boxV.pack_start(gtk.Label("Colors"))
		boxV.pack_start(self.check_red)
		boxV.pack_start(self.check_green)
		boxV.pack_start(self.check_blue)

		
	def __put_actions(self, box):
		""" Putting Actions """
		boxV = gtk.VBox()
		box.pack_start(boxV)
		boxV.pack_start(gtk.Label("Action"))
		boxH = gtk.HBox()
		boxV.pack_start(boxH)
		boxV1 = gtk.VBox()
		boxV2 = gtk.VBox()
		boxH.pack_start(boxV1)
		boxH.pack_start(gtk.VSeparator(), False)
		boxH.pack_start(boxV2)
		self.__put_radio_actions(boxV1)
		self.__put_button_actions(boxV2)
		
	def __put_radio_actions(self, box):
		self.radio_draw = gtk.RadioButton(None, "Draw")
		box.pack_start(self.radio_draw)
		
		self.radio_select = gtk.RadioButton(self.radio_draw, "Select")
		box.pack_start(self.radio_select)

		self.radio_select = gtk.RadioButton(self.radio_draw, "Move window")
		box.pack_start(self.radio_select)
			
	def __put_button_actions(self, box):
		boxH = gtk.HBox()
		box.pack_start(boxH)
		boxV1 = gtk.VBox()
		boxV2 = gtk.VBox()
		boxV3 = gtk.VBox()
		boxH.pack_start(boxV1)
		boxH.pack_start(gtk.VSeparator(), False)
		boxH.pack_start(boxV2)
		boxH.pack_start(gtk.VSeparator(), False)
		boxH.pack_start(boxV3)
		
		self.button_fill = gtk.Button("Fill")
		self.button_fill.connect("clicked", self.__clicked_fill)
		boxV1.pack_start(self.button_fill)
		
		self.button_group = gtk.Button("Group")
		self.button_group.connect("clicked", self.__clicked_group)
		boxV1.pack_start(self.button_group)
		
		self.button_ungroup = gtk.Button("Ungroup")
		self.button_ungroup.connect("clicked", self.__clicked_ungroup)
		boxV1.pack_start(self.button_ungroup)
		
		self.button_translate = gtk.Button("Translate")
		self.button_translate.connect("clicked", self.__clicked_translate)
		boxV2.pack_start(self.button_translate)
		
		self.button_rotate = gtk.Button("Rotate")
		self.button_rotate.connect("clicked", self.__clicked_rotate)
		boxV2.pack_start(self.button_rotate)
		
		self.button_reflect = gtk.Button("Reflect")
		self.button_reflect.connect("clicked", self.__clicked_reflect)
		boxV2.pack_start(self.button_reflect)
		
		self.button_change = gtk.Button("Change Selected")
		self.button_change.connect("clicked",self.__clicked_change)
		boxV3.pack_start(self.button_change)
	
################################ Event Listeners #############################################################

	def __clicked_save(self, save_button):
		""" Saving """
		print("Saving!")
		# self.drawing_controller.save_shapes()
	
	def __clicked_delete(self, delete_button):
		""" Deleting selected shapes """
		self.drawing_controller.delete_selected()
		self.selecting = False
	
	def __clicked_clearall(self, clear_button):
		""" Clear all function """
		self.drawing_controller.clear_all()
		self.canvas_box.set_mouse(False)
		self.canvas_box.points = []
		self.selecting = False
		self.operation = "none"
		
	def __clicked_fill(self, fill_button):
		""" Fill shapes funciton """
		self.get_spec()
		self.drawing_controller.fill_all(self.spec, True)
	
	
	def __clicked_group(self, group_button):
		""" Group shapes function """
		self.drawing_controller.group_all(self.spec)
			
	
	
	def __clicked_ungroup(self, ungroup_button):
		""" Ungroup shapes function """
		self.drawing_controller.ungroup_all(self.spec)
		
	def __clicked_translate(self, translate_button):
		""" Translate a group of selected Shapes """
		self.operation = "translate" 
	
	def __clicked_rotate(self, rotate_button):
		""" Rotate a group of selected Shapes """	
		self.operation = "rotate" 
	
	def __clicked_reflect(self, reflect_button):
		""" Reflect a group of selected shapes """
		self.operation = "reflect" 
	
	def __clicked_change(self, change_button):
		self.get_spec()
		self.drawing_controller.change_selected(self.spec)
	
	def __clicked_board(self, widget, event):
		""" Analyzes which action the user want and do it """
		action = [act for act in self.radio_draw.get_group() if act.get_active()][0].get_label()
		if action == "Draw":
			if self.operation == "none":
				self.drawing_controller.free_selected()
			if self.selecting and self.operation == "none":
				self.selecting = False
				return	# click to free selected shapes doesn't start the next but translate and rotate normal
			self.__drawing_click(widget, event)
		elif action == "Select":
			self.operation = "none"
			self.__select_click(widget, event)
			
		elif action == "Move window":
			self.spec.shape = "none"
			self.selecting = False
			self.__window_click(widget,event)

	def __move_on_board(self, widget, event):
		""" Analyzes which action the user want and do it """
		action = [act for act in self.radio_draw.get_group() if act.get_active()][0].get_label()
		if action == "Draw":
			self.__drawing_move(widget,event)
		elif action == "Select":
			pass
		elif action == "Move window":
			self.__window_move(widget,event)
			#pass
		
##################################### Not Drawing Actions ##########################################################

	def __select_click(self, widget, event):
		point = Point.Point(event.x, event.y)
		self.selecting = self.drawing_controller.select(point, self.selecting)
		self.canvas_box.points = []
##################################### Drawing Actions ##############################################################

	def __window_click(self,widget,event):
		
		if self.canvas_box.get_mouse():
			self.drawing_controller.w[0]+=event.x-self.canvas_box.points[0].x
			self.drawing_controller.w[1]+=event.y-self.canvas_box.points[0].y
			self.drawing_controller.w[2]+=event.x-self.canvas_box.points[0].x
			self.drawing_controller.w[3]+=event.y-self.canvas_box.points[0].y
			finished = self.drawing_controller.draw(self.spec,True)
			if(finished):
				self.canvas_box.points = []
			
		else:
			self.drawing_controller.free_selected()
			self.canvas_box.points.append(Point.Point(event.x, event.y))	
		self.canvas_box.change_mouse()

	def __window_move(self,widget,event):
		if self.canvas_box.get_mouse():
			self.drawing_controller.w[0]+=event.x-self.canvas_box.points[0].x
			self.drawing_controller.w[1]+=event.y-self.canvas_box.points[0].y
			self.drawing_controller.w[2]+=event.x-self.canvas_box.points[0].x
			self.drawing_controller.w[3]+=event.y-self.canvas_box.points[0].y
			finished = self.drawing_controller.draw(self.spec,False)
			self.drawing_controller.w[0]-=event.x-self.canvas_box.points[0].x
			self.drawing_controller.w[1]-=event.y-self.canvas_box.points[0].y
			self.drawing_controller.w[2]-=event.x-self.canvas_box.points[0].x
			self.drawing_controller.w[3]-=event.y-self.canvas_box.points[0].y

		return True		

	def __drawing_click(self, widget, event):
		""" Tells someone clicked inside the drawing area"""
		finished = True
		self.canvas_box.points.append(Point.Point(event.x, event.y))
		if self.operation == "rotate":
			win = EntryDialog.EntryDialog("Angulo (em graus): ", "360", True)
			win.show()
			gtk.main()
			angle = int(win.ret)
			finished = self.drawing_controller.rotate_all(True,-angle*math.pi/180.0,self.canvas_box.points[0])
			self.operation = "none"	
			self.canvas_box.points = []
			return True

		if self.canvas_box.get_mouse():
			self.get_spec()
			
			if self.operation=="translate":
				finished = self.drawing_controller.translate_all(True,self.canvas_box.points)		
			elif self.operation == "reflect":
				self.spec.points = self.canvas_box.points
				self.drawing_controller.reflect_by(self.spec)
			elif self.operation=="none": 
				#needed because line change the order of the points
				self.spec.points = [Point.Point(p.x, p.y) for p in self.canvas_box.points]
				# this if else makes possible draw figures that are defined by more than 2 points
				finished = self.drawing_controller.draw(self.spec,True)
			if(finished):
				self.canvas_box.points = []
			self.operation = "none"

		if(finished):
			self.canvas_box.change_mouse()
		return True

	def __drawing_move(self, widget, event):
		""" Moving inside the drawing area"""
		if self.canvas_box.get_mouse():
			self.get_spec()
			#needed because line change the order of the points 
			if self.operation=="none" or self.operation == "reflect":
				if self.operation == "reflect": # coxa!!!
					self.spec.shape = "Line"	
					self.spec.trace = "Normal"
					self.spec.double = False
					self.spec.color = [False, False, False]
				self.spec.points = [Point.Point(p.x, p.y) for p in self.canvas_box.points]
				self.spec.points.append(Point.Point(event.x, event.y))
				self.drawing_controller.draw(self.spec,False)
		return True		
		
	
	def get_spec(self):
		""" Set up self.spec using the information on buttons """
		self.spec = Spec.Spec()
		# Shapes
		self.spec.shape = [b for b in self.radio_line.get_group() if b.get_active()][0].get_label()
		# Trace
		self.spec.trace = [b for b in self.radio_normal.get_group() if b.get_active()][0].get_label()
		# Color
		self.spec.double = self.check_double.get_active()
		self.spec.color = [self.check_red.get_active(), self.check_green.get_active(), self.check_blue.get_active()]
		# Grouping
		self.spec.grouping = self.radio_select.get_active()
				
			
if __name__ == "__main__":
	win = Screen()
	win.show_all()
	gtk.main()
